package com.nus.iss.ad.dbhelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;

import com.nus.iss.ad.model.Employee;
import com.nus.iss.ad.utils.JsonParser;
import com.nus.iss.ad.utils.MyConfig;

public class DelegateDBHelper {
	
	private class addDelegatePara{
		
		public Date getStartDate() {
			return startDate;
		}

		public void setStartDate(Date startDate) {
			this.startDate = startDate;
		}

		public Date getEndDate() {
			return endDate;
		}

		public void setEndDate(Date endDate) {
			this.endDate = endDate;
		}

		public int getEmployeeId() {
			return employeeId;
		}

		public void setEmployeeId(int employeeId) {
			this.employeeId = employeeId;
		}
		private Date startDate;
		private Date endDate;
		private int employeeId;
		
		public addDelegatePara(){}
	}

	public List<Employee> getAllEmployeeInDepartment(){
		List<Employee> empList=new ArrayList<Employee>();
		String url=MyConfig.BACKEND_PATH + "/api/getAllEmployeeInDepartment";
		
		JSONArray arr=JsonParser.getJSONArrayFromUrl(url);
		try {
		for(int i=0;i<arr.length();i++){
			JSONObject obj;
			
				obj = arr.getJSONObject(i);
			
			Employee e=new Employee();
			e.setEmployeeId(obj.getInt("employeeId"));
			e.setName(obj.getString("name"));
			e.setLoginId(obj.getString("loginId"));
			empList.add(e);
		}
			} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return empList;
		
	}
	
	public void addDelegate(Date startDate,Date endDate,String empId){
		
		String url=MyConfig.BACKEND_PATH + "/api/addDelegate";
		
		SimpleDateFormat smf=new SimpleDateFormat("yyyy-MM-dd");
		String data=String.format("{startDate:'%s',endDate:'%s',employeeId:'%s'}",
				smf.format(startDate),
				smf.format(endDate),
				empId);
		
		JsonParser.postStream(url, data);
		
	}

	public void removeDelegate() {
String url=MyConfig.BACKEND_PATH + "/api/removeDelegate";
		

		JsonParser.postStream(url, "");
		
	}
}
